package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.AdvposDTO;
import com.ydd.dto.AdvposdPageDTO;
import com.ydd.entity.YddAdvImg;
import com.ydd.entity.YddAdvpos;

import com.ydd.mapper.YddAdvimgMapper;
import com.ydd.mapper.YddAdvposMapper;
import com.ydd.result.Result;
import com.ydd.service.YddAdvposService;

import com.ydd.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements YddAdvposService {


    @Autowired
    private YddAdvimgMapper advimgMapper;
    @Autowired
    private YddAdvposMapper advposMapper;

    /**
     * 广告位列表-分页
     *
     * @param dto
     * @return
     */
    @Override
    public Result findList(AdvposdPageDTO dto) {

        //获取广告ID
      List<Integer> advposIds =advposMapper.selectAdvposIds(dto);
        List<YddAdvpos> list=new ArrayList<>();
        for (Integer advposId : advposIds) {
            //广告图数量
            Long count = advimgMapper.selectCount(new LambdaQueryWrapper<YddAdvImg>().eq(YddAdvImg::getAdvimgpos, advposId));
            YddAdvpos advpos = getById(advposId);
            advpos.setAdvposnum(count.intValue());
            list.add(advpos);
        }
        IPage<YddAdvpos> page = new Page<>(dto.getPage(), dto.getLimit());
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(YddAdvpos::getAdvposname,dto.getAdvposname());
        page(page, wrapper);
        PageVO vo = PageVO.builder()
                .count((int) page.getTotal())
                .data(list)
                .totalPages((int) page.getPages())
                .pageSize((int) page.getSize())
                .currentPage((int) page.getCurrent())
                .build();
        return Result.success(vo);


    }

    /**
     * 根据id查询广告
     *
     * @return
     */
    @Override
    public Result selectByAdvposId(Long id) {
        YddAdvpos byId = getById(id);
        return Result.success(byId);
    }

    /**
     * 广告修改
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateAdvpos(AdvposDTO dto) {
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddAdvpos::getId, dto.getId());
        YddAdvpos advpos = new YddAdvpos();
        BeanUtils.copyProperties(dto, advpos);

        update(advpos, wrapper);
        return Result.success();
    }

    /**
     * 广告位删除
     *
     * @param id
     * @return
     */
    @Override
    public Result delAdvpos(Long id) {

        removeById(id);
        return Result.success();
    }

    /**
     * 广告位添加
     *
     * @param dto
     * @return
     */
    @Override
    public Result addAdvpos(AdvposDTO dto) {
        YddAdvpos advpos = new YddAdvpos();
        BeanUtils.copyProperties(dto, advpos);
        save(advpos);
        return Result.success();
    }

    /**
     * 广告位列表
     *
     * @return
     */
    @Override
    public Result all() {

        List<YddAdvpos> list = advposMapper.selectList(null);
        return Result.success(list);
    }
}
